﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using StudyEnglish.Model;
using System.ComponentModel;

namespace StudyEnglish
{
    public partial class VolAdd : PhoneApplicationPage
    {
        BackgroundWorker worker;
        int index,id;
        int countLst;
        List<string> lstDay;
        public VolAdd()
        {
            InitializeComponent();
           worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            worker.RunWorkerAsync();
           

        }
        private void LoadDay()
        {

            App.DayVM.GetMaxDaily(App.userID);
            countLst = App.DayVM.MaxDaily;
            lstDay = new List<string>();
            lstDay.Add("...");
            for (int i = 1; i <= countLst; i++)
            {
                lstDay.Add("Ngày " + i);
            }
           

        }
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            LoadDay();
       }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            lpkCountry.ItemsSource = lstDay;
            string selectedIndex, selectID;
            if (NavigationContext.QueryString.TryGetValue("index", out selectedIndex))
            {
                index = int.Parse(selectedIndex);
                lpkCountry.SelectedIndex = index;
                lpkCountry.IsEnabled = false;
            }
            if (NavigationContext.QueryString.TryGetValue("id", out selectID))
            {
                id = int.Parse(selectID);
                App.VolVM.GetVolByID(int.Parse(selectID));
                txt_newword.Text = App.VolVM.VolByID.NewWord;
                txt_pronounciation.Text = App.VolVM.VolByID.Pronounciation;
                txt_meaning.Text = App.VolVM.VolByID.Meaning;
            }
        }

        private bool checkDataInput()
        {
            if (txt_newword.Text.Length == 0)
            {
                MessageBox.Show("Hãy nhập từ vựng mới.", "Thông Báo", MessageBoxButton.OK);
                txt_newword.Focus();
                return false;
            }
            if (lpkCountry.SelectedIndex == -1)
            {
                MessageBox.Show("Hãy chọn ngày học", "Thông Báo", MessageBoxButton.OK);
                lpkCountry.Focus();
                return false;
            }
            else
            {
                if (!NavigationContext.QueryString.ContainsKey("id"))
                {
                    App.VolVM.GetVolByWord(txt_newword.Text);
                    if (App.VolVM.countVolByWord > 0)
                    {
                        MessageBox.Show("Từ vựng này đã có rùi", "Thông Báo", MessageBoxButton.OK);
                        txt_newword.Focus();
                        return false;
                    }
                }
            }
            return true;
        }

        private void resetControl()
        {
            txt_newword.Text = "";
            txt_pronounciation.Text = "";
            txt_meaning.Text = "";
            if (lpkCountry.IsEnabled)
               lpkCountry.SelectedIndex = 0;
        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
            
        }
        private void InsProcess()
        {
            //insert volcabulary
            AppVocabulary vol = new AppVocabulary
            {
                NewWord = txt_newword.Text,
                Pronounciation = txt_pronounciation.Text,
                Meaning = txt_meaning.Text
            };
            App.VolVM.AddVol(vol);

            App.VolVM.GetVolIDMax();
            App.DayVM.GetCountMaxDaily(App.userID, countLst);
            int dayItem;
            if (lpkCountry.SelectedIndex == 0)
            {
                if (App.DayVM.CountMaxDaily >= App.Volnum)
                    dayItem = App.DayVM.MaxDaily + 1;
                else
                    dayItem = App.DayVM.MaxDaily;
            }
            else
            {
                dayItem = int.Parse(lpkCountry.SelectedItem.ToString().Replace("Ngày ", ""));
            }
            //insert daily
            AppDaily daily = new AppDaily
            {
                UserID = App.userID,
                Day = dayItem,
                VocabularyID = App.VolVM.VolIdMax
            };
            App.DayVM.AddDaily(daily);
            if (MessageBox.Show("Bạn có muốn nhập thêm từ vựng khác không?", "Thông Báo", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                resetControl();
            }
            else
            {
                if (NavigationService.CanGoBack)
                    NavigationService.GoBack();
            }
        }

        private void UpdProcess(string id)
        {
            //App.VolVM.GetVolByID(int.Parse(id));
            AppVocabulary itemVol = new AppVocabulary();//App.VolVM.VolByID;
            itemVol.ID = int.Parse(id);
            itemVol.NewWord = txt_newword.Text;
            itemVol.Pronounciation = txt_pronounciation.Text;
            itemVol.Meaning = txt_meaning.Text;
            App.VolVM.UpdVol(itemVol);
            int dayItem;
            if (lpkCountry.SelectedIndex == 0)
            {
                if (App.DayVM.CountMaxDaily >= App.Volnum)
                    dayItem = App.DayVM.MaxDaily + 1;
                else
                    dayItem = App.DayVM.MaxDaily;
            }
            else
            {
                dayItem = int.Parse(lpkCountry.SelectedItem.ToString().Replace("Ngày ", ""));
            }
            AppDaily itemDay = new AppDaily();
            itemDay.Day = dayItem;
            itemDay.UserID = App.userID;
            itemDay.VocabularyID = itemVol.ID;
            App.DayVM.UpdDaily(itemDay);
            if (NavigationService.CanGoBack)
                NavigationService.GoBack();

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (checkDataInput())
            {
                if (NavigationContext.QueryString.ContainsKey("id"))
                {
                    string selectedID;
                    NavigationContext.QueryString.TryGetValue("id", out selectedID);
                    UpdProcess(selectedID);

                }
                else
                {
                    InsProcess();
                }

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (NavigationService.CanGoBack)
                NavigationService.GoBack();
        }

        //private void logout_Click(object sender, RoutedEventArgs e)
        //{
        //    NavigationService.Navigate(new Uri("/UserLogin.xaml", UriKind.Relative));
        //}

        
    }
}